#include "../headers.h"

int PONG_LONG_RAQUETTE = 9;

void pong_main(void) {
	int score[2] = {0, 0};
	int etatJeu = 0;
	int posRaqu1 = PONG_HAUTEUR / 2 - PONG_LONG_RAQUETTE / 2;
	int posRaqu2 = PONG_HAUTEUR / 2 - PONG_LONG_RAQUETTE / 2;
	int event = 0;
	int sensRaqu1 = PONG_STOP;
	int sensRaqu2 = PONG_STOP;
	int difficulte = 0;
	int modeJeu = 0;
	int continuer = 1;
	int choix = 0;
	Pong_vect vPuck = {PONG_V_X_DEPART, PONG_V_Y_DEPART}, posPuck = {20, 20};
	CSLP_CLOCK clockPuck, clockRaqu;
	double dernierTRaquette = 0, dernierTPuck = 0;

	pong_menu_pong(&difficulte, &modeJeu);
	clear();

	if(modeJeu == 3) {
		nodelay(stdscr, 0);
		clear();
		return;
	}
	else if(difficulte == 1)
		PONG_LONG_RAQUETTE = 11;
	else if(difficulte == 2)
		PONG_LONG_RAQUETTE = 9;
	else if(difficulte == 3)
		PONG_LONG_RAQUETTE = 7;

	pong_init();
	pong_afficher_position(modeJeu);
	CSLP_ResetClock(&clockPuck);
	CSLP_ResetClock(&clockRaqu);
	dernierTPuck = CSLP_GetElapsedTime(clockPuck);
	dernierTRaquette = CSLP_GetElapsedTime(clockRaqu);

	/**/
	goto debut;/**/

	while(1) {
		etatJeu = 0;

		event = pong_recup_event();

		if(event == '0') { /* QUIT */
			clear();
			choix = afficher_menu("Oui\nNon\n", "Voulez-vous vraiment quitter la partie en cours ?\n", "PONG");

			if(choix == 1) {
				nodelay(stdscr, 0);
				clear();
				return;
			}
			else {
				clear();
				pong_init();
				pong_afficher_position(modeJeu);
				pong_afficher(pong_fpos_to_ipos(posPuck), score);
			}
		}

		if(modeJeu == 1) {
			if(event == KEY_UP)
				sensRaqu2 = PONG_HAUT;
			else if(event == KEY_DOWN)
				sensRaqu2 = PONG_BAS;
			else if(event == KEY_RETURN)
				sensRaqu2 = PONG_STOP;
			else if(event == 'h')
				sensRaqu1 = PONG_HAUT;
			else if(event == 'b')
				sensRaqu1 = PONG_BAS;
			else if(event == ' ')
				sensRaqu1 = PONG_STOP;
		}
		else if(modeJeu == 2) {
			if(vPuck.x > 0)
				sensRaqu2 = pong_action_IA(pong_fpos_to_ipos(posPuck), posRaqu2, difficulte);
			else
				sensRaqu2 = pong_retour_centre_IA(pong_fpos_to_ipos(posPuck), posRaqu2);

			if(event == KEY_UP)
				sensRaqu1 = PONG_HAUT;
			else if(event == KEY_DOWN)
				sensRaqu1 = PONG_BAS;
			else if(event == KEY_RETURN)
				sensRaqu1 = PONG_STOP;
		}


		if(CSLP_GetElapsedTime(clockRaqu) - dernierTRaquette > PONG_VITESSE_RAQUETTE / 1000.0 / difficulte) {
			dernierTRaquette = CSLP_GetElapsedTime(clockRaqu);
			pong_bouger_raquettes(sensRaqu1, sensRaqu2, &posRaqu1, &posRaqu2);
		}

		if(CSLP_GetElapsedTime(clockPuck) - dernierTPuck > PONG_VITESSE_PUCK / 1000.0 / difficulte) {
			dernierTPuck = CSLP_GetElapsedTime(clockPuck);
			etatJeu = pong_bouger_puck(&posPuck, posRaqu1, posRaqu2, &vPuck);
		}

		if(etatJeu == PONG_PERDU_1) {
			jouer_son(PONGBALLEPERDUE);
			napms(1000);
			sensRaqu1 = PONG_STOP;
			sensRaqu2 = PONG_STOP;
			score[1] += 1;

			if(score[1] == 10) {
				Pong_pos pos10 = {(PONG_POS_TERRAIN_X + PONG_LARGEUR) / 2 + 11, PONG_POS_TERRAIN_Y + 2};
				pong_afficher_vide(pos10);
				pong_afficher_chiffre(pos10, 1);
				pos10.x += 5;
				pong_afficher_chiffre(pos10, 0);
				refresh();
				pong_afficher_victoire(2);
				score[0] = 0;
				score[1] = 0;
				pong_afficher_vide(pos10);


				clear();
				choix = afficher_menu_recommencer("PONG");

				if(choix == 1) {
					clear();
					pong_init();
					pong_afficher(pong_fpos_to_ipos(posPuck), score);
				}
				else {
					nodelay(stdscr, 0);
					clear();
					return;
				}
			}

/**/debut:/**/
			posPuck.x = PONG_POS_RAQU_X_1 - PONG_POS_TERRAIN_X + 1;
			posPuck.y = posRaqu1 + PONG_LONG_RAQUETTE / 2;
			vPuck.x = PONG_V_X_DEPART;
			vPuck.y = PONG_V_Y_DEPART;

			while((event != ' ' && modeJeu == 1) || (event != KEY_RETURN && modeJeu == 2)) {
				event = getch();

				if(event == '0') { /* QUIT */
					clear();
					choix = afficher_menu("Oui\nNon\n", "Voulez-vous vraiment quitter la partie en cours ?\n", "PONG");

					if(choix == 1) {
						nodelay(stdscr, 0);
						clear();
						return;
					}
					else {
						clear();
						pong_init();
						pong_afficher_position(modeJeu);
						pong_afficher(pong_fpos_to_ipos(posPuck), score);
					}
				}
				else if(modeJeu == 1) {
					if(event == KEY_UP)
						sensRaqu2 = PONG_HAUT;
					else if(event == KEY_DOWN)
						sensRaqu2 = PONG_BAS;
					else if(event == KEY_RETURN)
						sensRaqu2 = PONG_STOP;
					else if(event == 'h')
						sensRaqu1 = PONG_HAUT;
					else if(event == 'b')
						sensRaqu1 = PONG_BAS;
				}
				else if(modeJeu == 2) {
					if(event == KEY_UP)
						sensRaqu1 = PONG_HAUT;
					else if(event == KEY_DOWN)
						sensRaqu1 = PONG_BAS;
				}

				if(CSLP_GetElapsedTime(clockRaqu) - dernierTRaquette > PONG_VITESSE_RAQUETTE / 1000.0 / difficulte) {
					dernierTRaquette = CSLP_GetElapsedTime(clockRaqu);
					pong_bouger_raquettes(sensRaqu1, sensRaqu2, &posRaqu1, &posRaqu2);
					posPuck.y = posRaqu1 + PONG_LONG_RAQUETTE / 2;
				}

				pong_afficher(pong_fpos_to_ipos(posPuck), score);
				CSLP_Sleep(1);
			}

			jouer_son(PONGRAQUETTE);
		}
		else if(etatJeu == PONG_PERDU_2) {
			jouer_son(PONGBALLEPERDUE);
			napms(1000);
			continuer = 1;
			sensRaqu1 = PONG_STOP;
			sensRaqu2 = PONG_STOP;
			score[0] += 1;

			if(score[0] == 10) {
				Pong_pos pos10 = {(PONG_POS_TERRAIN_X + PONG_LARGEUR) / 2 - 19, PONG_POS_TERRAIN_Y + 2};
				pong_afficher_chiffre(pos10, 1);
				pos10.x += 5;
				pong_afficher_vide(pos10);
				pong_afficher_chiffre(pos10, 0);
				refresh();
				pong_afficher_victoire(1);
				score[0] = 0;
				score[1] = 0;
				pos10.x -= 5;
				pong_afficher_vide(pos10);


				clear();
				choix = afficher_menu_recommencer("PONG");

				if(choix == 1) {
					clear();
					pong_init();
					pong_afficher(pong_fpos_to_ipos(posPuck), score);
				}
				else {
					nodelay(stdscr, 0);
					clear();
					return;
				}
			}

			posPuck.x = PONG_POS_RAQU_X_2 - PONG_POS_TERRAIN_X - 3;
			posPuck.y = posRaqu2 + PONG_LONG_RAQUETTE / 2;
			vPuck.x = -PONG_V_X_DEPART;
			vPuck.y = PONG_V_Y_DEPART;

			if(modeJeu == 2)
				pong_random_IA(posRaqu1, 1);

			while(continuer) {
				event = getch();

				if(event == '0') { /* QUIT */
					clear();
					choix = afficher_menu("Oui\nNon\n", "Voulez-vous vraiment quitter la partie en cours ?\n", "PONG");

					if(choix == 1) {
						nodelay(stdscr, 0);
						clear();
						return;
					}
					else {
						clear();
						pong_init();
						pong_afficher_position(modeJeu);
						pong_afficher(pong_fpos_to_ipos(posPuck), score);
					}
				}
				else if(modeJeu == 1) {
					if(event == KEY_UP)
						sensRaqu2 = PONG_HAUT;
					else if(event == KEY_DOWN)
						sensRaqu2 = PONG_BAS;
					else if(event == KEY_RETURN)
						continuer = 0;
					else if(event == 'h')
						sensRaqu1 = PONG_HAUT;
					else if(event == 'b')
						sensRaqu1 = PONG_BAS;
					else if(event == ' ')
						sensRaqu1 = PONG_STOP;
				}
				else if(modeJeu == 2) {
					if(event == KEY_UP)
						sensRaqu1 = PONG_HAUT;
					else if(event == KEY_DOWN)
						sensRaqu1 = PONG_BAS;
					else if(event == KEY_RETURN)
						sensRaqu1 = PONG_STOP;

					sensRaqu2 = pong_random_IA(posRaqu2, 0);

					if(sensRaqu2 == PONG_STOP)
						continuer = 0;
				}

				if(CSLP_GetElapsedTime(clockRaqu) - dernierTRaquette > PONG_VITESSE_RAQUETTE / 1000.0 / difficulte) {
					dernierTRaquette = CSLP_GetElapsedTime(clockRaqu);
					pong_bouger_raquettes(sensRaqu1, sensRaqu2, &posRaqu1, &posRaqu2);
					posPuck.y = posRaqu2 + PONG_LONG_RAQUETTE / 2;
				}

				etatJeu = 0;
				pong_afficher(pong_fpos_to_ipos(posPuck), score);
				CSLP_Sleep(1);
			}

			jouer_son(PONGRAQUETTE);
			sensRaqu2 = PONG_STOP;
		}

		pong_afficher(pong_fpos_to_ipos(posPuck), score);
		CSLP_Sleep(1);
	}
}

/******************************/

void pong_init(void) {
	int i;

	nodelay(stdscr, 1);
	clear();
	afficher_titre("PONG");
	CSLP_ColorText(COLOR_WHITE);

	move(PONG_POS_TERRAIN_Y, PONG_POS_TERRAIN_X);

	for(i = 0; i <= PONG_LARGEUR + 1; i++)
		addch(PONG_PLEIN);

	move(PONG_POS_TERRAIN_Y + PONG_HAUTEUR + 1, PONG_POS_TERRAIN_X);

	for(i = 0; i <= PONG_LARGEUR + 1; i++)
		addch(PONG_PLEIN);

	for(i = 0; i <= PONG_HAUTEUR + 1; i++) {
		if(i % 2 == 1)
			mvaddch(i + PONG_POS_TERRAIN_Y, (PONG_POS_TERRAIN_X + PONG_LARGEUR) / 2, PONG_PLEIN);
	}
}

/******************************/

void pong_afficher(Pong_pos posPuck, int *score) {
	static int xAvant = 1, yAvant = 1, scoreAvant[2] = {0};
	Pong_pos posPong_chiffre = {(PONG_POS_TERRAIN_X + PONG_LARGEUR) / 2 - 14, PONG_POS_TERRAIN_Y + 2};
	int i;

	if(scoreAvant[0] != score[0])
		pong_afficher_vide(posPong_chiffre);

	pong_afficher_chiffre(posPong_chiffre, score[0]);
	posPong_chiffre.x = (PONG_POS_TERRAIN_X + PONG_LARGEUR) / 2 + 11;

	if(scoreAvant[1] != score[1])
		pong_afficher_vide(posPong_chiffre);

	pong_afficher_chiffre(posPong_chiffre, score[1]);
	scoreAvant[0] = score[0];
	scoreAvant[1] = score[1];

	mvaddch(yAvant + PONG_POS_TERRAIN_Y, xAvant + PONG_POS_TERRAIN_X, ' ');
	mvaddch(PONG_POS_TERRAIN_Y + (yAvant = posPuck.y + 1), PONG_POS_TERRAIN_X + (xAvant = posPuck.x + 1), PONG_SIGNE_PUCK);

	for(i = 0; i <= PONG_HAUTEUR + 1; i++) {
		if(i % 2 == 1)
			mvaddch(i + PONG_POS_TERRAIN_Y, (PONG_POS_TERRAIN_X + PONG_LARGEUR) / 2, PONG_PLEIN);
	}

	refresh();
}

/******************************/

void pong_afficher_chiffre(Pong_pos pos, int chiffre) {
	int i, j;
	static char chiffresTab[10][20] = {
		{1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1},/* 0 */
		{0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1},/* 1 */
		{1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1},/* 2 */
		{1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1},/* 3 */
		{0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1},/* 4 */
		{1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1},/* 5 */
		{1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1},/* 6 */
		{1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0},/* 7 */
		{1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1},/* 8 */
		{1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1},/* 9 */
	};

	for(i = 0; i < 5; i++) {
		move(pos.y + i, pos.x);

		for(j = 0; j < 4; j++) {
			if(chiffresTab[chiffre % 10][i * 4 + j])
				addch(PONG_PLEIN);
			else
				move(pos.y + i, pos.x + j + 1);
		}
	}
}

/******************************/

void pong_afficher_vide(Pong_pos pos) {
	int i, j;

	for(i = 0; i < 5; i++) {
		move(pos.y + i, pos.x);

		for(j = 0; j < 4; j++)
			addch(' ');
	}
}

/******************************/

int pong_bouger_puck(Pong_vect *posPuck, int posRaqu1, int posRaqu2, Pong_vect *vDepl) {
	int posRaquX1 = PONG_POS_RAQU_X_1 - PONG_POS_TERRAIN_X;
	int posRaquX2 = PONG_POS_RAQU_X_2 - PONG_POS_TERRAIN_X - 2;

	posRaqu1--;
	posRaqu2--;

	if(posPuck->x >= posRaquX1 && posPuck->x + vDepl->x <= posRaquX1 && pong_arrondi(posPuck->y) >= posRaqu1 && pong_arrondi(posPuck->y) <= posRaqu1 + PONG_LONG_RAQUETTE) {
		posPuck->x += posRaquX1 + -vDepl->x - (posPuck->x - posRaquX1);
		vDepl->x = -vDepl->x;
		jouer_son(PONGRAQUETTE);

		if(PONG_MODIFIER_VECTEUR) {
			if(posPuck->x + 0.5 < posRaqu1 + PONG_LONG_RAQUETTE / 2.0)
				vDepl->x += (PONG_LONG_RAQUETTE / 4.0 - (posPuck->x - posRaqu1)) / 50;
			else
				vDepl->x += (PONG_LONG_RAQUETTE * 3 / 4.0 - (posPuck->x - posRaqu1)) / 50;

			if(vDepl->x > 1.7)
				vDepl->x = 1.7;
			else if(vDepl->x < 0.7)
				vDepl->x = 0.7;

			vDepl->y = (vDepl->y / pong_val_absolue(vDepl->y)) * (2 - vDepl->x);
		}
	}
	else if(posPuck->x <= posRaquX2 && posPuck->x + vDepl->x >= posRaquX2 &&
					pong_arrondi(posPuck->y) >= posRaqu2 && pong_arrondi(posPuck->y) <= posRaqu2 + PONG_LONG_RAQUETTE) {
		posPuck->x = posRaquX2 - (vDepl->x - (posRaquX2 - posPuck->x));
		vDepl->x = -vDepl->x;
		jouer_son(PONGRAQUETTE);

		if(PONG_MODIFIER_VECTEUR) {
			if(posPuck->x + 0.5 < posRaqu2 + PONG_LONG_RAQUETTE / 2.0)
				vDepl->x += (PONG_LONG_RAQUETTE / 4.0 - (posPuck->x - posRaqu2)) / 50;
			else
				vDepl->x += (PONG_LONG_RAQUETTE * 3 / 4.0 - (posPuck->x - posRaqu2)) / 50;

			if(vDepl->x < -1.7)
				vDepl->x = -1.7;
			else if(vDepl->x > -0.7)
				vDepl->x = -0.7;

			vDepl->y = (vDepl->y / pong_val_absolue(vDepl->y)) * (2 + vDepl->x);
		}
	}

	else if(((posPuck->x + 1) + vDepl->x < PONG_LARGEUR && vDepl->x > 0) || (posPuck->x > -vDepl->x && vDepl->x < 0))
		posPuck->x += vDepl->x;
	else {
		if(vDepl->x > 0)
			return PONG_PERDU_2;
		else if(vDepl->x < 0)
			return PONG_PERDU_1;
	}

	if(((posPuck->y + 1) + vDepl->y < PONG_HAUTEUR && vDepl->y > 0) || (posPuck->y > -vDepl->y && vDepl->y < 0))
		posPuck->y += vDepl->y;
	else {
		if(vDepl->y > 0)
			posPuck->y = (PONG_HAUTEUR - vDepl->y) + (PONG_HAUTEUR - posPuck->y) - 2;
		else if(vDepl->y < 0)
			posPuck->y = -(vDepl->y + posPuck->y);

		vDepl->y = -vDepl->y;
		jouer_son(PONGCOTE);
	}

	return 0;
}

/******************************/

int pong_arrondi(double nombre) {
	return (int)((int) nombre + (nombre - (int) nombre > 0.5));
}

/******************************/

Pong_pos pong_fpos_to_ipos(Pong_vect pos) {
	Pong_pos retour;

	retour.x = pong_arrondi(pos.x);
	retour.y = pong_arrondi(pos.y);

	return retour;
}

/******************************/

double pong_val_absolue(double nb) {
	if(nb < 0)
		return -nb;
	else
		return nb;
}

/******************************/

int pong_recup_event(void) {
	int c, retour = 0;

	c = getch();

	switch(c) {
	case KEY_UP:
		retour = KEY_UP;
		break;
	case KEY_DOWN:
		retour = KEY_DOWN;
		break;
	case 'h':
		retour = 'h';
		break;
	case 'b':
		retour = 'b';
		break;
	case KEY_RETURN:
		retour = KEY_RETURN;
		break;
	case ' ':
		retour = ' ';
		break;
	case '0':
		retour = '0';
		break;
	default :
		break;
	}

	return retour;
}

/******************************/

void pong_bouger_raquettes(int sensRaqu1, int sensRaqu2, int *raquY1, int *raquY2) {
	int i;

	if(sensRaqu1 == PONG_HAUT && *raquY1 > 1) {
		mvaddch(PONG_POS_TERRAIN_Y + *raquY1 + PONG_LONG_RAQUETTE - 1, PONG_POS_RAQU_X_1, ' ');
		*raquY1 -= 1;
	}
	else if(sensRaqu1 == PONG_BAS && *raquY1 + PONG_LONG_RAQUETTE <= PONG_HAUTEUR) {
		mvaddch(PONG_POS_TERRAIN_Y + *raquY1, PONG_POS_RAQU_X_1, ' ');
		*raquY1 += 1;
	}

	if(sensRaqu2 == PONG_HAUT && *raquY2 > 1) {
		mvaddch(PONG_POS_TERRAIN_Y + *raquY2 + PONG_LONG_RAQUETTE - 1, PONG_POS_RAQU_X_2, ' ');
		*raquY2 -= 1;
	}
	else if(sensRaqu2 == PONG_BAS && *raquY2 + PONG_LONG_RAQUETTE <= PONG_HAUTEUR) {
		mvaddch(PONG_POS_TERRAIN_Y + *raquY2, PONG_POS_RAQU_X_2, ' ');
		*raquY2 += 1;
	}

	for(i = 0; i < PONG_LONG_RAQUETTE; i++) {
		mvaddch(PONG_POS_TERRAIN_Y + *raquY1 + i, PONG_POS_RAQU_X_1, PONG_PLEIN);
		mvaddch(PONG_POS_TERRAIN_Y + *raquY2 + i, PONG_POS_RAQU_X_2, PONG_PLEIN);
	}
}

/******************************/

void pong_afficher_victoire(int quiGagne) {
	int i, j;
	Pong_pos posText1 = {(PONG_POS_TERRAIN_X + PONG_LARGEUR) / 2 + (quiGagne == 1 ? -35 : 17),
											 PONG_POS_TERRAIN_Y + PONG_HAUTEUR / 2 - 1
											};
	Pong_pos posText2 = {(PONG_POS_TERRAIN_X + PONG_LARGEUR) / 2 + (quiGagne == 1 ? 20 : -35),
											 PONG_POS_TERRAIN_Y + PONG_HAUTEUR / 2 - 1
											};

	/* W */
	mvaddch(posText1.y, posText1.x, PONG_PLEIN);
	mvaddch(posText1.y + 1, posText1.x, PONG_PLEIN);
	mvaddch(posText1.y + 2, posText1.x, PONG_PLEIN);
	mvaddch(posText1.y + 3, posText1.x, PONG_PLEIN);
	mvaddch(posText1.y + 3, posText1.x + 1, PONG_PLEIN);
	mvaddch(posText1.y + 4, posText1.x + 1, PONG_PLEIN);
	mvaddch(posText1.y + 4, posText1.x + 2, PONG_PLEIN);
	mvaddch(posText1.y + 2, posText1.x + 3, PONG_PLEIN);
	mvaddch(posText1.y + 3, posText1.x + 3, PONG_PLEIN);
	mvaddch(posText1.y + 4, posText1.x + 3, PONG_PLEIN);
	mvaddch(posText1.y + 4, posText1.x + 4, PONG_PLEIN);
	mvaddch(posText1.y + 3, posText1.x + 5, PONG_PLEIN);
	mvaddch(posText1.y + 4, posText1.x + 5, PONG_PLEIN);
	mvaddch(posText1.y, posText1.x + 6, PONG_PLEIN);
	mvaddch(posText1.y + 1, posText1.x + 6, PONG_PLEIN);
	mvaddch(posText1.y + 2, posText1.x + 6, PONG_PLEIN);
	mvaddch(posText1.y + 3, posText1.x + 6, PONG_PLEIN);
	refresh();
	CSLP_Sleep(100);

	/* I */
	mvaddch(posText1.y, posText1.x + 8, PONG_PLEIN);
	mvaddch(posText1.y + 4, posText1.x + 8, PONG_PLEIN);
	mvaddch(posText1.y, posText1.x + 9, PONG_PLEIN);
	mvaddch(posText1.y + 1, posText1.x + 9, PONG_PLEIN);
	mvaddch(posText1.y + 2, posText1.x + 9, PONG_PLEIN);
	mvaddch(posText1.y + 3, posText1.x + 9, PONG_PLEIN);
	mvaddch(posText1.y + 4, posText1.x + 9, PONG_PLEIN);
	mvaddch(posText1.y, posText1.x + 10, PONG_PLEIN);
	mvaddch(posText1.y + 4, posText1.x + 10, PONG_PLEIN);
	refresh();
	CSLP_Sleep(100);

	/* N */
	mvaddch(posText1.y, posText1.x + 12, PONG_PLEIN);
	mvaddch(posText1.y + 1, posText1.x + 12, PONG_PLEIN);
	mvaddch(posText1.y + 2, posText1.x + 12, PONG_PLEIN);
	mvaddch(posText1.y + 3, posText1.x + 12, PONG_PLEIN);
	mvaddch(posText1.y + 4, posText1.x + 12, PONG_PLEIN);
	mvaddch(posText1.y + 1, posText1.x + 13, PONG_PLEIN);
	mvaddch(posText1.y + 2, posText1.x + 14, PONG_PLEIN);
	mvaddch(posText1.y + 3, posText1.x + 15, PONG_PLEIN);
	mvaddch(posText1.y, posText1.x + 16, PONG_PLEIN);
	mvaddch(posText1.y + 1, posText1.x + 16, PONG_PLEIN);
	mvaddch(posText1.y + 2, posText1.x + 16, PONG_PLEIN);
	mvaddch(posText1.y + 3, posText1.x + 16, PONG_PLEIN);
	mvaddch(posText1.y + 4, posText1.x + 16, PONG_PLEIN);
	refresh();
	jouer_son(GAGNE);
	CSLP_Sleep(2500);



	/* L */
	mvaddch(posText2.y, posText2.x, PONG_PLEIN);
	mvaddch(posText2.y + 1, posText2.x, PONG_PLEIN);
	mvaddch(posText2.y + 2, posText2.x, PONG_PLEIN);
	mvaddch(posText2.y + 3, posText2.x, PONG_PLEIN);
	mvaddch(posText2.y + 4, posText2.x, PONG_PLEIN);
	mvaddch(posText2.y + 4, posText2.x + 1, PONG_PLEIN);
	mvaddch(posText2.y + 4, posText2.x + 2, PONG_PLEIN);
	mvaddch(posText2.y + 4, posText2.x + 3, PONG_PLEIN);
	refresh();
	CSLP_Sleep(100);

	/* O */
	mvaddch(posText2.y, posText2.x + 5, PONG_PLEIN);
	mvaddch(posText2.y + 1, posText2.x + 5, PONG_PLEIN);
	mvaddch(posText2.y + 2, posText2.x + 5, PONG_PLEIN);
	mvaddch(posText2.y + 3, posText2.x + 5, PONG_PLEIN);
	mvaddch(posText2.y + 4, posText2.x + 5, PONG_PLEIN);
	mvaddch(posText2.y, posText2.x + 6, PONG_PLEIN);
	mvaddch(posText2.y + 4, posText2.x + 6, PONG_PLEIN);
	mvaddch(posText2.y, posText2.x + 7, PONG_PLEIN);
	mvaddch(posText2.y + 4, posText2.x + 7, PONG_PLEIN);
	mvaddch(posText2.y, posText2.x + 8, PONG_PLEIN);
	mvaddch(posText2.y + 1, posText2.x + 8, PONG_PLEIN);
	mvaddch(posText2.y + 2, posText2.x + 8, PONG_PLEIN);
	mvaddch(posText2.y + 3, posText2.x + 8, PONG_PLEIN);
	mvaddch(posText2.y + 4, posText2.x + 8, PONG_PLEIN);
	refresh();
	CSLP_Sleep(100);

	/* S */
	mvaddch(posText2.y, posText2.x + 10, PONG_PLEIN);
	mvaddch(posText2.y + 1, posText2.x + 10, PONG_PLEIN);
	mvaddch(posText2.y + 2, posText2.x + 10, PONG_PLEIN);
	mvaddch(posText2.y + 4, posText2.x + 10, PONG_PLEIN);
	mvaddch(posText2.y, posText2.x + 11, PONG_PLEIN);
	mvaddch(posText2.y + 2, posText2.x + 11, PONG_PLEIN);
	mvaddch(posText2.y + 4, posText2.x + 11, PONG_PLEIN);
	mvaddch(posText2.y, posText2.x + 12, PONG_PLEIN);
	mvaddch(posText2.y + 2, posText2.x + 12, PONG_PLEIN);
	mvaddch(posText2.y + 4, posText2.x + 12, PONG_PLEIN);
	mvaddch(posText2.y, posText2.x + 13, PONG_PLEIN);
	mvaddch(posText2.y + 2, posText2.x + 13, PONG_PLEIN);
	mvaddch(posText2.y + 3, posText2.x + 13, PONG_PLEIN);
	mvaddch(posText2.y + 4, posText2.x + 13, PONG_PLEIN);
	refresh();
	CSLP_Sleep(100);

	/* E */
	mvaddch(posText2.y, posText2.x + 15, PONG_PLEIN);
	mvaddch(posText2.y + 1, posText2.x + 15, PONG_PLEIN);
	mvaddch(posText2.y + 2, posText2.x + 15, PONG_PLEIN);
	mvaddch(posText2.y + 3, posText2.x + 15, PONG_PLEIN);
	mvaddch(posText2.y + 4, posText2.x + 15, PONG_PLEIN);
	mvaddch(posText2.y, posText2.x + 16, PONG_PLEIN);
	mvaddch(posText2.y + 2, posText2.x + 16, PONG_PLEIN);
	mvaddch(posText2.y + 4, posText2.x + 16, PONG_PLEIN);
	mvaddch(posText2.y, posText2.x + 17, PONG_PLEIN);
	mvaddch(posText2.y + 2, posText2.x + 17, PONG_PLEIN);
	mvaddch(posText2.y + 4, posText2.x + 17, PONG_PLEIN);
	mvaddch(posText2.y, posText2.x + 18, PONG_PLEIN);
	mvaddch(posText2.y + 4, posText2.x + 18, PONG_PLEIN);
	jouer_son(PERDU);
	refresh();
	CSLP_Sleep(2500);

	for(i = 0; i < 5; i++) {
		move(posText1.y + i, posText1.x);

		for(j = 0; j < 17; j++)
			addch(' ');

		move(posText2.y + i, posText2.x);

		for(j = 0; j < 19; j++)
			addch(' ');

		refresh();
		CSLP_Sleep(100);
	}

	while(getch() != ERR)
		;
}

/******************************/

void pong_menu_pong(int *difficulte, int *modeJeu) {
	clear();
	int choix = 0;
	char menu1[] = "Jouer seul\n"
								 "Jouer à deux\n"
								 "Retour\n";
	char menu2[] = "Lent\n"
								 "Moyen\n"
								 "Rapide\n"
								 "Retour\n";
	char menu3[] = "Facile\n"
								 "Moyen\n"
								 "Difficile\n"
								 "Retour\n";

	while(choix != 3) {
		clear();
		choix = afficher_menu_jeu(menu1, "Mode de jeu");

		if(choix == 1) {
			clear();
			choix = afficher_menu_jeu(menu3, "Force de l'ordi");
			*difficulte = choix;
			*modeJeu = 2;

			if(choix != 4)
				return;
		}
		else if(choix == 2) {
			clear();
			choix = afficher_menu_jeu(menu2, "Vitesse");
			*difficulte = choix;
			*modeJeu = 1;

			if(choix != 4)
				return;
		}
	}

	*modeJeu = 3;
}

/******************************/

int pong_action_IA(Pong_pos posPuck, int posRaquY, int force) {
	int retour = PONG_STOP;
	static int dest = 1000;
	static CSLP_CLOCK clockNewRand;
	static double dernierT = 0;

	if(dest == 1000) {
		CSLP_ResetClock(&clockNewRand);
		dest = posPuck.y - PONG_LONG_RAQUETTE / 2;
	}

	if(CSLP_GetElapsedTime(clockNewRand) - dernierT > 0.05) {
		if(force == 1 && rand() % 3 == 0)
			dest = posPuck.y - PONG_LONG_RAQUETTE / 2;
		else if(force == 2 && rand() % 60 < 53)
			dest = posPuck.y - PONG_LONG_RAQUETTE / 2;
		else if(force == 3 && rand() % 60 < 58)
			dest = posPuck.y - PONG_LONG_RAQUETTE / 2;

		dernierT = CSLP_GetElapsedTime(clockNewRand);
	}

	if(posRaquY < dest)
		retour = PONG_BAS;
	else if(posRaquY > dest)
		retour = PONG_HAUT;

	return retour;
}

/******************************/

int pong_retour_centre_IA(Pong_pos posPuck, int posRaquY) {
	int retour = PONG_STOP;

	if(posPuck.x < PONG_LARGEUR / 2 && posRaquY < PONG_HAUTEUR / 2 - PONG_LONG_RAQUETTE / 2)
		retour = PONG_BAS;
	else if(posPuck.x < PONG_LARGEUR / 2 && posRaquY > PONG_HAUTEUR / 2 - PONG_LONG_RAQUETTE / 2)
		retour = PONG_HAUT;

	return retour;
}

/******************************/

int pong_random_IA(int posRaquY, int bNew) {
	static int dest = 0;
	static int nbFeintes = 0;
	int retour = PONG_STOP;

	if(bNew) {
		dest = rand() % (PONG_HAUTEUR - PONG_LONG_RAQUETTE) + 1;
		nbFeintes = rand() % 10;
	}

	if(posRaquY < dest)
		retour = PONG_BAS;
	else if(posRaquY > dest)
		retour = PONG_HAUT;
	else {
		if(nbFeintes > 0) {
			nbFeintes--;
			dest = rand() % (PONG_HAUTEUR - PONG_LONG_RAQUETTE) + 1;

			if(posRaquY < dest)
				retour = PONG_BAS;
			else if(posRaquY > dest)
				retour = PONG_HAUT;
		}
	}

	return retour;
}

/******************************/

void pong_afficher_position(int modeJeu) {

	if(modeJeu == 2) {
		CSLP_Color(CT, COLOR_WHITE);
		mvaddstr(39, 0, "└───────────────────────────────────────────────────────────────┴────────────────────────────────────────────────────────────────┘");
		CSLP_Color(COLOR_RED, COLOR_WHITE);
		mvaddstr(39, DEFAULT_SCREEN_SIZE_X / 4 - strlen_utf8(prenom) / 4, prenom);
		CSLP_Color(COLOR_GREEN, COLOR_WHITE);
		mvaddstr(39, DEFAULT_SCREEN_SIZE_X - (DEFAULT_SCREEN_SIZE_X / 4 - strlen_utf8("ORDINATEUR") / 4), "ORDINATEUR");
		CSLP_Color(COLOR_WHITE, COLOR_BLACK);
	}
	else if(modeJeu == 1) {
		CSLP_Color(CT, COLOR_WHITE);
		mvaddstr(39, 0, "└───────────────────────────────────────────────────────────────┴────────────────────────────────────────────────────────────────┘");
		CSLP_Color(COLOR_RED, COLOR_WHITE);
		mvaddstr(39, DEFAULT_SCREEN_SIZE_X / 4 - strlen_utf8(prenom) / 4, prenom);
		CSLP_Color(COLOR_GREEN, COLOR_WHITE);
		mvaddstr(39, DEFAULT_SCREEN_SIZE_X - (DEFAULT_SCREEN_SIZE_X / 4 - strlen_utf8("JOUEUR 2") / 4), "JOUEUR 2");
		CSLP_Color(COLOR_WHITE, COLOR_BLACK);
	}
}

